<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use App\Zan;
use App\Comment;

class PostController extends Controller
{
    /*
     * 文章列表
     */
    public function index()
    {
        // $user = \Auth::user();
        $posts = Post::orderBy('created_at', 'desc')->withCount(["zans", "comments"])->paginate(6);

        return view('post/index', compact('posts'));
    }


    public function imageUpload(Request $request)
    {
        $path = $request->file('wangEditorH5File')->storePublicly(md5(\Auth::id() . time()));
        return asset('storage/'. $path);
    }

    public function create()
    {
        // $user = \Auth::user();
        return view('post/create');
    }
    //保存文章
    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|max:50|min:4',
            'content' => 'required|min:10',
        ]);
        
        $params = array_merge(request(['title', 'content']), ['user_id' => \Auth::id()]);
        Post::create($params);
        return redirect('/posts');
    }

    public function edit(Post $post)
    {
        // $user = \Auth::user();
        return view('post/edit', compact('post'));
    }

    //文章详情
    public function show(\App\Post $post)
    {
        // $user = \Auth::user();
        $post->load('comments');//预加载,这样view层不会再查询
        return view('post/show', compact('post'));
    }

    public function update(Request $request, Post $post)
    {//更新文章
        $this->validate($request, [
            'title' => 'required|max:255|min:4',
            'content' => 'required|min:100',
        ]);

        $this->authorize('update', $post);

        $post->update(request(['title', 'content']));
        return redirect("/posts/{$post->id}");
    }
    //删除文章
     public function delete(Post $post)
     {
        $post->delete();
        return redirect("/posts");
     }
    /*
     * 文章评论保存
     */
    public function comment()
    {
        $this->validate(request(),[
            'post_id' => 'required|exists:posts,id',
            'content' => 'required|min:10',
        ]);

        $user_id = \Auth::id();

        $params = array_merge(
            request(['post_id', 'content']),
            compact('user_id')
        );
        Comment::create($params);
        return back();
    }

    /*
     * 点赞
     */
    public function zan(Post $post)
    {
//        $zan = new Zan;
//        $zan->user_id = \Auth::id();
//        $post->zans()->save($zan);
//        return back();
        $param=[
            'user_id'=>\Auth::id(),
            'post_id'=>$post->id,
        ];
        Zan::firstOrCreate($param);
        return back();
    }

    /*
     * 取消点赞
     */
    public function unzan(Post $post)
    {
        $post->zan(\Auth::id())->delete();
        return back();
    }

    /*
     * 搜索结果页
     */
    public function search()
    {
        $this->validate(request(),[
            'query' => 'required'
        ]);
        // $user = \Auth::user();

        $query = request('query');
        $posts = Post::search(request('query'))->paginate(2);
        return view('post/search', compact('posts', 'query'));
    }


}
